<?php
/**
 * Created by Command.
 * User: Vito
 */

namespace app\admin\crud\system_role;

use app\admin\crud\Edit;
use app\exception\MessageException;

class SystemRoleEdit extends Edit
{
    use SystemRoleTrait;

    //显示的字段
    protected $field = [];
    //查询条件
    protected $where = [];
    //关联
    protected $with = null;
    //追加字段
    protected $append = ['auth'];
    //追加数据回调
    protected $appendCallback = [];

    protected $labelCallback = [
        'get_auth_label' => ['name' => '权限', 'key' => 'auth'],
    ];

    protected function editMiddleware($next, $model)
    {
        if (!$this->hasRoleToDo($model)) {
            throw new MessageException('无权限获取该角色');
        }

        return parent::editMiddleware($next, $model); // TODO: Change the autogenerated stub
    }

    public function getAuthLabel()
    {
        $detail = $this->getData('detail');

        //有上级则获取上级的权限列表
        if ($detail && $detail->pid) {
            $role = $this->getLogic()->where('id', $detail->pid)->find();
        } else {
            $role = $this->user->getUserInfo()->role;
        }

        $result = $role->auth_tree;

        return $result;
    }
}
